package io.yizoo.business.controller;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.yizoo.business.entity.*;
import io.elm.core.CoreController;
import io.elm.core.CoreMapper;
import io.elm.core.HMapper;
import io.yizoo.system.entity.sys_org;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/pmis/pj")
public class pmis_pjController extends CoreController {

    @Autowired
    public io.yizoo.business.mapper.biz_pmis_pjMapper biz_pmis_pjMapper;

    @Override
    protected void Init() {
        PowerEntity = biz_pmis_pj.class;
        PowerUse = EnumSet.of(Power.IDDelete, Power.IDAny, Power.IDGetSingle,Power.IDUpsert);
    }

    @PostMapping("/GetData")
    public Object GetData(@RequestBody String jsonStr) {
        List<?> ret =  biz_pmis_pjMapper.GetFullData(
                Ctor.DMaker(jsonStr).Selector
                        .IsWhere("and t.name like concat('%', #{name}, '%') ", "name")
                        .IsWhere("and t.org =#{org}","org")
                        .IsWhere("and t.manager=#{manager}","manager")
                        .IsWhere("and t.type=#{type}","type")
                        .IsPage()
                        .GetFactor()
        );
        return JsonResultPage(ret);
    }

    @PostMapping("/GetInsStageData")
    public Object GetInsStageData(@RequestBody String jsonStr) {
        HMapper.PageData ret = Ctor.HMapper.GetTreePro(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_pmis_pj_ins_stage.class)
                        .Where("and pj_id=#{pj_id}","pj_id")
                        .GetFactor(), "stage_id", "pid"
        );
        if(ret.getSelectData().size()>0)
        {
            return JsonResult(ret);
        }
        else {
            biz_pmis_pjMapper.Initpj_ins_stage(
                    Ctor.DMaker(jsonStr).Selector.GetFactor()
            );
            return JsonResult(
                    Ctor.HMapper.GetTreePro(Ctor.DMaker(jsonStr).Selector
                            .From(biz_pmis_pj_ins_stage.class)
                            .Where("and pj_id=#{pj_id}","pj_id")
                            .GetFactor(), "stage_id", "pid")
            );
        }
    }

    @PostMapping("/UpsertPjAndInsStage")
    public Object UpsertPjAndInsStage(@RequestBody String jsonStr) {
        //更新主表
        ObjectNode node = Ctor.getObjectNode(jsonStr);
        Object ret = Ctor.HMapper.Upsert(biz_pmis_pj.class, node, null);
        //更新子表
        Ctor.HMapper.Save(biz_pmis_pj_ins_stage.class,
                (ArrayNode) node.get("ins_stages"),
                Ctor.DMaker().Deleter
                        .putFactorItem("pj_id",node.get("id").asText())
                        .From(biz_pmis_pj_ins_stage.class)
                        .Where("and pj_id=#{pj_id}","pj_id")
                        .GetFactor());
        return JsonResult(ret != null ? 1 : 0);
    }

    @PostMapping("/GetList")
    public Object GetList(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_pmis_pj.class, "a")
                        .Select("a.*, a.name as select_name, a.sys_code_key as select_id")
                        .IsWhere("and sys_code_key like concat('%', #{sys_code_key}, '%')", "sys_code_key")
                        .IsWhere("and name like concat('%', #{select_name}, '%')", "select_name")
                        .OrderBy("a_date desc")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/GetTree")
    public Object GetTree(@RequestBody String jsonStr) throws Exception {
        Map<String, Object> factor = Ctor.DMaker(jsonStr).Selector
                .From(biz_pmis_pj_ins_stage.class)
                .Where("and pj_id=#{pj_id}","pj_id")
                .IsWhere("and name like CONCAT('%', #{name}, '%')","name")
                .IsPage()
                .GetFactor();
        HMapper.PageData data = Ctor.HMapper.GetTreePro(factor,"stage_id", "pid");
        return JsonResult(data);
    }

    @PostMapping("/GetPath")
    public Object GetPath(@RequestBody String jsonStr) {
        Object data = Ctor.HMapper.GetPath(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_pmis_pj_ins_stage.class)
                        .Select("id,pid,name")
                        .IsWhere("and id=#{id}","id")
                        .GetFactor());
        return JsonResult(data);
    }

    @PostMapping("/DeletePj")
    public Object DeletePj(@RequestBody String jsonStr) throws Exception {
        int task = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_pmis_pj_task.class)
                        .Where("and pj_id=#{id}", "id")
                        .GetFactor());
        int ins = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_pmis_pj_ins_stage.class)
                        .Where("and pj_id=#{id}", "id")
                        .GetFactor());
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_pmis_pj.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }
}